package tu.java.utils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import tu.java.db.DBConnManager;

public class UIUtils {
	
	public static String printOptions(String sql, int selected) {
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		
		String options = "";
		
		try {
			conn = DBConnManager.getInstance().getConnection();
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			while (rs.next()) {
				options += "\t<option value=\"" + rs.getInt(1) + "\"" + (selected == rs.getInt(1) ? "selected" : "") + ">" + rs.getString(2) + "</option>\n";
			}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		finally {
			DBConnManager.getInstance().close(rs);
			DBConnManager.getInstance().close(stmt);
			DBConnManager.getInstance().freeConnection(conn);
		}
		
		return options;
	}
	
}
